package com.papper.common.enums.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.papper.common.utils.tool.StringTool;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通用方法工具类
 *
 * @author 小懒虫
 * @date 2018/10/15
 */
public class EnumsUtil {
    /**
     * 将枚举转成List集合
     *
     * @param enumClass 枚举类
     */
    public static JSONArray enumToMap(Class<?> enumClass) {
        JSONArray jsonArray = new JSONArray();
        try {
            Object[] objects  = enumClass.getEnumConstants();
            Method   getCode  = enumClass.getMethod("getCode");
            Method   getValue = enumClass.getMethod("getValue");
            for (Object obj : objects) {
                Object     iCode      = getCode.invoke(obj);
                Object     iValue     = getValue.invoke(obj);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", iCode);
                jsonObject.put("value", iValue);
                jsonArray.add(jsonObject);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
        }
        return jsonArray;
    }

    /**
     * 根据枚举code获取枚举对象
     *
     * @param enumClass 枚举类
     * @param code      code值
     */
    public static Object enumCode(Class<?> enumClass, Object code) {
        try {
            Object[] objects = enumClass.getEnumConstants();
            Method   getCode = enumClass.getMethod("getCode");
            for (Object obj : objects) {
                Object iCode = getCode.invoke(obj);
                if (iCode.equals(code)) {
                    return obj;
                }
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        }
        return "";
    }

    /**
     * 根据枚举code获取枚举对象
     *
     * @param enumClass 枚举类
     * @param code      code值
     */
    public static String enumValue(Class<?> enumClass, Object code) {
        try {
            Object[] objects = enumClass.getEnumConstants();
            Method   getCode = enumClass.getMethod("getCode");
            for (Object obj : objects) {
                String iCode    = StringTool.nullToEmpty(getCode.invoke(obj));
                String compCode = StringTool.nullToEmpty(code);
                if (iCode.equals(compCode)) {
                    Method getValue = enumClass.getMethod("getValue");
                    if (getValue != null) {
                        Object iMessage = getValue.invoke(obj);
                        return String.valueOf(iMessage);
                    } else {
                        return null;
                    }
                }
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        }
        return null;
    }

    /**
     * 根据枚举code获取枚举对象
     *
     * @param enumClass 枚举类
     * @param code      code值
     */
    public static String enumDesc(Class<?> enumClass, Object code) {
        try {
            Object[] objects = enumClass.getEnumConstants();
            Method   getCode = enumClass.getMethod("getCode");
            for (Object obj : objects) {
                Object iCode = getCode.invoke(obj);
                if (iCode.equals(code)) {
                    Method getDesc = enumClass.getMethod("getDesc");
                    if (getDesc != null) {
                        Object iMessage = getDesc.invoke(obj);
                        return String.valueOf(iMessage);
                    } else {
                        return null;
                    }
                }
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        }
        return null;
    }
}
